// packageB/pages/reservation/reservation.js
// import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNum: 1,
    pageSize: 5,
    total: '',
    latitude: '',
    longitude: ''
  },
  goback() {
    wx.navigateBack({
      delta: 1
    })
  },
  reservation(e) {
    let that = this
    let isAgencyCode = wx.getStorageSync('userInfo').agencyCode
    console.log(isAgencyCode, 'isAgencyCode')
    if (isAgencyCode) {
      const visitCorpId = e.currentTarget.dataset.proitem.visitCorpId
      const userId = wx.getStorageSync('userInfo').userId
      const params = {
        userId: userId,
        visitCorpId: visitCorpId,
      }
      params.token = wx.getStorageSync('token');
      params.assignType = 'person';
      params.assignId = userId
      params.selectCheckbox = [userId];
      params.corpKey=e.currentTarget.dataset.proitem.corpKey
     
      console.log(params, 'paramsv')
      getApp().globalData.request(
        '/visit/corp_assign_qd',
        params,
        "POST"
      ).then(res => {
        console.log(res, 'res');
        if (res.code==200) {
          wx.showToast({
            title: '分配成功,请移步至走访企业查看详情',
            icon: 'none',
            duration: 1500
          })
          that.setData({
            pageNum: 1,
            list: [],
          })
        }else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          })
        }
        this.getList()
      })





      // getApp().globalData.request(
      //   '/corpVisit/seize_orders', params,
      //   "POST"
      // ).then(res => {
      //   console.log(res, 'eee')
      //   let msg = res.msg
      //   let code = res.code
      //   wx.navigateTo({
      //     url: `/pages/appointmentResults/appointmentResults?msg=${msg}&code=${code}`,
      //   });
      // })
    } else {
      wx.navigateTo({
        url: '/packageA/pages/notBind/notBind?type=3'
      })
    }
  },
  getList() {
    const params = {
      pageBO: {
        pageSize:this.data.pageSize,
        pageNum: this.data.pageNum,
      },
      searchParam: this.data.serach_pro,
      // fileName:this.data.fileName,
      bankId: wx.getStorageSync("userInfo").agencyCode,
      userLat: this.transMarker([this.data.latitude, this.data.longitude])[0],
      userLng: this.transMarker([this.data.latitude, this.data.longitude])[1],
    }
    console.log(params, 'paramsparams')
    getApp().globalData.request(
      '/map/lookVisitCorpAround', params,
      "POST"
    ).then(res => {
      console.log(res, '列表')
      if (res.data.list.length > 0) {
        // 距离保留整数
        res.data.list.forEach(function (item, index) {
          if (item.shortName) {
            item.shortName1 = item.shortName.slice(0, 2)
            item.shortName2 = item.shortName.slice(2, 4)
          }
        });

        const list = this.data.list.concat(res.data.list)
        // 设置标题颜色
        for (let i in list) {
          if (i % 4 === 0) {
            list[i].backColor = '#8BCBB8'
          } else if (i % 3 === 0) {
            list[i].backColor = '#99C1F2'
          } else if (i % 5 === 0) {
            list[i].backColor = '#B8AAD6'
          } else {
            list[i].backColor = '#D99D9A'
          }

        }
        this.setData({
          list: list,
          total: res.data.total
        })
      }
    })
  },
  details(e) {
    console.log(e)
    const corpKey = e.currentTarget.dataset.proitem.corpKey
    const latitude = e.currentTarget.dataset.proitem.latitude
    const longitude = e.currentTarget.dataset.proitem.longitude
    const distance = e.currentTarget.dataset.proitem.distance
    wx.navigateTo({
      url: `/pages/mapDetail/mapDetail?corpKey=${corpKey}&latitude=${latitude}&longitude=${longitude}&distance=${distance}`,
    });
  },
  scrollBottom() {
    console.log(this.data.list.length, this.data.total, '11')
    if (this.data.list.length < this.data.total) {
      // this.onBottom();
      this.setData({
        pageNum: ++this.data.pageNum,
      });
      this.getList();
    }
  },
  phone(event) {
    console.log(event.currentTarget.id);
    if (event.currentTarget.id) {
      wx.makePhoneCall({
        phoneNumber: event.currentTarget.id,
      });
    } else {
      wx.showToast({
        icon: "none",
        title: "暂无电话",
      });
    }
  },
  // 坐标系转化
  transMarker(item) {
    const resae = gcoord.transform(
      [item[0], item[1]],
      gcoord.GCJ02, // 当前坐标系
      gcoord.BD09 // 目标坐标系
    );
    return resae;
  },
  location(e) {
    console.log(e)
    const latitude = e.currentTarget.dataset.proitem.latitude
    const longitude = e.currentTarget.dataset.proitem.longitude
    const name = e.currentTarget.dataset.proitem.d2CorpName
    const address = e.currentTarget.dataset.proitem.d2Dom
    const scale = e.currentTarget.dataset.proitem.scale
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      name: name,
      address: address,
      scale: scale,
    });
    // wx.getLocation({
    //   type: 'wgs84',
    //   success(res) {

    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log('纬度' + res.latitude)
        console.log('经度' + res.longitude)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude

        })
        that.getList()
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('11')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
